#!/bin/bash                                                                                                                 

declare -A memcached_url=()
declare -A memcached_port=()
memcached_update_every=3
memcached_priority=60000

declare -a memcached_response=()
memcached_curr_connections=0
memcached_cmd_get=0
memcached_cmd_set=0
memcached_threads=0
memcached_curr_items=0
memcached_total_items=0
memcached_get_hits=0
memcached_get_hit_rate=0
memcached_get() {
    echo stats | nc $memcached_url $memcached_port > /tmp/memcached.tmp &
    
    sleep 1
    [ -z /tmp/memcached.tmp ] && return 1
    memcached_response=($(cat /tmp/memcached.tmp|tr -d '\r'|awk -F ' ' '{print $3}'))

    memcached_curr_connections="${memcached_response[8]}"
    memcached_cmd_get="${memcached_response[12]}"
    memcached_cmd_set="${memcached_response[13]}"
    memcached_threads="${memcached_response[39]}"
    memcached_curr_items="${memcached_response[50]}"
    memcached_total_items="${memcached_response[51]}"
    memcached_get_hits="${memcached_response[16]}"
    if [ -z "${memcached_curr_connections}" \
            -o -z "{memcached_cmd_get}" \
            -o -z "{memcached_cmd_set}" \
            -o -z "{memcached_threads}" \
            -o -z "{memcached_curr_items}" \
            -o -z "{memcached_total_items}" \
            -o -z "{memcached_get_hits}" \
            ]
            then
            echo >&2 "memcached:empty values got from memcached server"
            return 1
    fi
    killall nc && return 0
}

memcached_check() {
       
        memcached_get
        if [ $? -ne 0 ]
                then
                echo >&2 "memcached: cannot find stats on '${memcached_url}' '${memcached_port}'. Please set memcached_url and memcached_port in $confd/memcached.conf"
                return 1
        fi
        return 0
}

memcached_create() {
            cat <<EOF
CHART memcached_local.curr_connections '' "memcached current connections" "connections" memcached memcached.curr_connections line $((memcached_priority + 1)) $memcached_update_every
DIMENSION connections '' absolute 1 1

CHART memcached_local.get&set_status '' "memcached get&set status" "status" memcached memcached.status line $((memcached_priority + 1)) $memcached_update_every
DIMENSION cmd_get '' absolute 1 1
DIMENSION cmd_set '' absolute 1 1

CHART memcached_local.threads '' "memcached current threads" "threads" memcached memcached.threads line $((memcached_priority + 1)) $memcached_update_every
DIMENSION threads '' absolute 1 1

CHART memcached_local.items '' "memcached items" "items" memcached memcached.items line $((memcached_priority + 1)) $memcached_update_every
DIMENSION total_items '' absolute 1 1
DIMENSION current_items '' absolute 1 1

CHART memcached_local.get_hits '' "memcached get hits" "hits" memcached memcached.get_hits line $((memcached_priority + 1)) $memcached_update_every
DIMENSION get_hits '' absolute 1 1

EOF

        return 0
}

memcached_update() {

            memcached_get || return 1
cat <<VALUESEOF
BEGIN memcached_local.curr_connections $1
SET connections = $((memcached_curr_connections))
END
BEGIN memcached_local.get&set_status $1
SET cmd_get = $((memcached_cmd_get))
SET cmd_set = $((memcached_cmd_set))
END
BEGIN memcached_local.threads $1
SET threads = $((memcached_threads))
END
BEGIN memcached_local.items $1
SET current_items = $((memcached_curr_items))
SET total_items = $((memcached_total_items))
END
BEGIN memcached_local.get_hits $1
SET get_hits = $((memcached_get_hits))
END
VALUESEOF
            return 0
